package com.quick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quick.base.KeyValue;
import com.quick.base.Page;
import com.quick.base.Search;
import com.quick.mapper.SysRoleMapper;
import com.quick.service.ISysRoleMenuService;
import com.quick.service.ISysRoleService;
import com.quick.sys.domain.dto.SysRoleDTO;
import com.quick.sys.domain.pojo.SysRole;
import com.quick.sys.domain.search.SysRoleSearch;
import com.quick.sys.domain.vo.SysRoleVo;
import com.quick.util.Convert;
import com.quick.util.PageUtil;
import com.quick.util.QuickTool;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SysRoleServiceImpl implements ISysRoleService {

    private final SysRoleMapper sysRoleMapper;
    private final ISysRoleMenuService roleMenuService;
    @Override
    public Page<SysRoleVo> search(Search<SysRoleSearch> search) {
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper(queryWrapper,search.getSearch());
        List<SysRole> sysRoleList = sysRoleMapper.selectList(queryWrapper);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoleList);
        return PageUtil.page(pageInfo,SysRoleVo.class);
    }

	@Override
    public Map<Integer, SysRoleVo> loadRole(List<Integer> roleIds) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysRole::getId, roleIds);
        List<SysRole> sysRoleList = sysRoleMapper.selectList(queryWrapper);
        List<SysRoleVo> sysRoleVos = Convert.convert(sysRoleList,SysRoleVo.class);
        Map<Integer,SysRoleVo> sysRoleVoMap = sysRoleVos.stream().collect(Collectors.toMap(SysRoleVo::getId,o->o));
        return sysRoleVoMap;
    }

	@Override
	public Boolean saveSysRole(SysRoleDTO role) {
		SysRole entity = Convert.convert(role, SysRole.class);
		if(QuickTool.notNull(role.getIds())) {
			entity.setIds(QuickTool.joiner(",", role.getIds()));
		}else {
			entity.setIds("");
		}
		if(QuickTool.isNull(role.getId())) {
			QuickTool.setFillInsert(entity);
			int row =  sysRoleMapper.insert(entity);
			if(row>0) {
				roleMenuService.saveRoleMenu(role.getIds(),entity.getId());
				return true;
			}
			return false;
		}
		QuickTool.setFillUpdate(entity);
		int row = sysRoleMapper.updateById(entity);
		if(row>0) {
			roleMenuService.saveRoleMenu(role.getIds(),entity.getId());
			return true;
		}
		return false;
	}
	@Override
	public SysRoleVo queryById(Long id) {
		SysRole role = sysRoleMapper.selectById(id);
		return Convert.convert(role, SysRoleVo.class);
	}

	@Override
	public Boolean delById(Long id) {
		SysRole role = sysRoleMapper.selectById(id);
		role.setIsDelete("01");
		QuickTool.setFillUpdate(role);
		return sysRoleMapper.updateById(role)>0;
	}

	@Override
	public Boolean batchDel(List<Long> ids) {
		return sysRoleMapper.deleteBatchIds(ids)>0;
	}

	@Override
	public List<KeyValue> loadAll() {
		 List<KeyValue> values = new ArrayList<KeyValue>();
		 QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
	     List<SysRole> roles = sysRoleMapper.selectList(queryWrapper);
	     if(QuickTool.notNull(roles)) {
	    	 roles.forEach(e->{
	    		 values.add(new KeyValue(e.getName(), e.getId()));
	    	 }); 
	     }
		return values;
	}

	@Override
	public List<Integer> selectMenuByRoles(List<Integer> roleIds) {
		List<Integer> integers = new ArrayList<Integer>();
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(SysRole::getId, roleIds);
		List<SysRole> roles = sysRoleMapper.selectList(queryWrapper);
		for (SysRole role : roles) {
			List<Integer> roleList = Arrays.stream(role.getIds().split(",")).map(s->Integer.parseInt(s.trim())).collect(Collectors.toList());
			integers.addAll(roleList);
		}
		return integers;
	}
}
